﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.WebControls;
using System.IO;
using System.Data;

namespace ERPWebDemos.Tools
{
    public partial class DeleteRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            bool success = false;

            //删除表的记录
            string tableName = Request["table"];
            string filter = Request["filter"];
            if (tableName != null && tableName != string.Empty && filter !=null &&　filter !=string.Empty )
            {
                string sqlText = "delete from dbo.["+tableName +"] where "+filter ;
                int count = RuleCode.Common.SqlHelper.Instance.ExecuteNonQuery(System.Data.CommandType.Text, sqlText);
                success =(count >0);
            }

            //删除Query设置
            string query_name = Request["queryname"];
            string query_tablename = Request["tablename"];
            string query_id = Request["id"];
            string query_refid = Request["refid"];
            string query_type = Request["type"];
            if (!String.IsNullOrEmpty(query_id) && !String.IsNullOrEmpty(query_name) && !String.IsNullOrEmpty(query_refid)
                && !String.IsNullOrEmpty(query_tablename) && !String.IsNullOrEmpty(query_type))
            {
                string queryXmlFilePath = RuleQueryTable.getXMLFileName(query_tablename , Request["id"]);
                string queryXmlFileName = Path.Combine(queryXmlFilePath, query_name + ".xml");
                if (File.Exists(queryXmlFileName))
                {
                    DataSet dSet = new DataSet();
                    dSet.ReadXml(queryXmlFileName);
                    try
                    {
                        if (dSet.Tables.Count > 0)
                        {

                            DataTable datatable = null;
                            if (query_type == "f")                     
                                datatable = dSet.Tables[RuleQueryTable.RangeDataTableName];   
                            else if(query_type =="s") 
                                datatable = dSet.Tables[RuleQueryTable.SortingDataTableName];
                            if (datatable != null)
                            {
                                DataRow[] allRowss = datatable.Select("ID="+query_refid );
                                foreach (DataRow row in allRowss)
                                {
                                    datatable.Rows.Remove(row);
                                }

                                File.Delete(queryXmlFileName);
                                dSet.WriteXml(queryXmlFileName);
                                success = true;
                            }
                        }
                    }
                    catch (Exception error)
                    {                    
                    }
                }
                
            }


            Response.Write(success);
            Response.End();
        }
    }
}
